package edu.chl.dat255.bluebanana.presentation.impl;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.Gallery;
import edu.chl.dat255.bluebanana.R;
import edu.chl.dat255.bluebanana.data.impl.DbConst;
import edu.chl.dat255.bluebanana.presentation.IBasicProfileSettings;
import edu.chl.dat255.bluebanana.utils.DatabaseUtil;
import edu.chl.dat255.bluebanana.utils.ImageAdapter;

public class BasicProfileSettings extends Activity implements
		IBasicProfileSettings {
	private EditText profileNameField;
	private Gallery gallery;
	private long profileId;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.basicprofilesettings);
		
		profileId = getIntent().getLongExtra("Profile", -1);

		profileNameField = (EditText) findViewById(R.id.profileNameField);
		profileNameField.setText(DatabaseUtil.getTextValue(profileId, DbConst.KEY_NAME, this));

		gallery = (Gallery) findViewById(R.id.imageGallery);
		gallery.setAdapter(new ImageAdapter(this));
		
	

		gallery.setOnItemClickListener(new OnItemClickListener() {
			@SuppressWarnings("rawtypes")
			@Override
			public void onItemClick(AdapterView parent, View v, int position, long viewId) {
				DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_ICON_PATH, position, BasicProfileSettings.this);
				v.setFocusable(true);
				v.setFocusableInTouchMode(true);
				v.requestFocus();	
			}
		});
	}
	
	
	@Override
	protected void onResume() {
		super.onResume();
		gallery.setSelection(DatabaseUtil.getIntValue(profileId, DbConst.KEY_ICON_PATH, this)); 
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		String name = profileNameField.getText().toString();
		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_NAME, name, BasicProfileSettings.this);
	}
}
